﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;  
using Generator.Helpers;
using Generator.Service;
using System.Configuration;

namespace Generator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Appuyer sur une touche pour executer le generateur de VSTemplate...");
            Console.ReadKey();

            // Récupération des variables applicatives
            var source = ConfigurationManager.AppSettings["SourceFolder"];
            var target = ConfigurationManager.AppSettings["TargetFolder"];
            var zipFilename = ConfigurationManager.AppSettings["ZipFilename"];

#if GENERATE
            Console.WriteLine();
            // Suppression du répertoire cible avant génération
            if (Directory.Exists(target))
            {
                Console.WriteLine("Suppression des anciens fichiers...");
                Directory.GetFiles(target, "*.*", SearchOption.AllDirectories).ToList().ForEach(f => FileHelper.RemoveReadOnlyAttribute(f));
                //Directory.Delete(target, true);
            }
            Console.WriteLine("Generation en cours...");
            Directory.CreateDirectory(target);

            // Génération du Template
            var generator = new VSSolutionTemplateGenerator();
            generator.Execute(source, target);

#endif
            // Nettoyage de la solution 
            foreach (var file in Directory.GetFiles(target, "*.*scc", SearchOption.AllDirectories)) {
                FileHelper.RemoveReadOnlyAttribute(file);
                File.Delete(file);
            }

            foreach (var projectFile in Directory.GetFiles(target, "*.csproj", SearchOption.AllDirectories))
            {
                // http://schemas.microsoft.com/developer/msbuild/2003
                var xdoc = XDocument.Load(projectFile);
                var ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
                //var element = xdoc.Descendants(ns + "PropertyGroup").First();

                (new string[] { "SccProjectName", "SccLocalPath", "SccAuxPath", "SccProvider" }).ToList().ForEach(
                    s => {
                        var elementToDelete = xdoc.Descendants(ns + s).FirstOrDefault();
                        if (elementToDelete != null)
                        {
                            elementToDelete.Remove();
                        }
                    }
                 );
                
                //element.Element("SccProjectName").Remove();
                //element.Element("SccLocalPath").Remove();
                //element.Element("SccAuxPath").Remove();
                //element.Element("SccProvider").Remove();

                xdoc.Save(projectFile);
            }

            Console.WriteLine(); 
            Console.WriteLine("Generation du zip en cours...");
            // Zip le contenu 
            if (File.Exists(zipFilename))
            {
                FileHelper.RemoveReadOnlyAttribute(zipFilename);
                File.Delete(zipFilename);
            }
            ZipHelper help = new ZipHelper();
            help.ZipFolder(target, zipFilename);

            // Fin
            Console.WriteLine(); 
            Console.WriteLine("Generation terminee.");
            Console.WriteLine(); 
            Console.WriteLine("Appuyer sur une touche...");
            Console.ReadKey();
        }
    }
}
